package com.video.tiktokdownloader.nowatermark.bean

import androidx.paging.DataSource
import androidx.room.*
import java.io.Serializable

/**
 * @author Afra55
 * @date 2020/6/24
 * A smile is the best business card.
 * 没有成绩，连呼吸都是错的。
 */
@Entity(tableName = "question")
data class MediaEntity(
    @PrimaryKey @ColumnInfo(name = "a1") val id: String,
    @ColumnInfo(name = "a2") val what: Int // 0 video, 1 music
):Serializable {
    @ColumnInfo(name = "a3")
    var name: String? = null
    @ColumnInfo(name = "a4")
    var cover: String? = null
    @ColumnInfo(name = "a5")
    var desc: String? = null
    @ColumnInfo(name = "a6")
    var userCover: String? = null
    @ColumnInfo(name = "a7")
    var createTime: String? = null
    @ColumnInfo(name = "a8")
    var downloadUrl: String? = null
    @ColumnInfo(name = "a9", defaultValue = "0")
    var savedTime: Long = 0
    @ColumnInfo(name = "a10")
    var userName: String? = null
    @ColumnInfo(name = "a11")
    var fileName: String? = null
    @ColumnInfo(name = "a12")
    var userId: String? = null

    @ColumnInfo(name = "a13")
    var originUrl: String? = null
}

@Dao
interface MyDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAll(media: MediaEntity)

    @Query("SELECT * FROM question WHERE a1=:mediaId")
    fun getMediaById(mediaId: String): MediaEntity?

    @Query("SELECT * FROM question ORDER BY a9 DESC")
    fun pageAllMedia(): DataSource.Factory<Int, MediaEntity>

    @Query("SELECT * FROM question WHERE a2=0 ORDER BY a9 DESC")
    fun pageAllVideoSavedMedia(): DataSource.Factory<Int, MediaEntity>

    @Query("SELECT * FROM question WHERE a2=1 ORDER BY a9 DESC")
    fun pageAllMusicSavedMedia(): DataSource.Factory<Int, MediaEntity>

    @Delete
    fun delete(mediaEntity: MediaEntity)
}

@Database(entities = arrayOf(MediaEntity::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun dao(): MyDao
}